package com.emep.changzhi.analyse.http.equipment;

import com.emep.changzhi.analyse.entity.equipment.EquipmentMoniter;
import com.emep.changzhi.analyse.model.ResultBean;
import com.emep.changzhi.analyse.service.equipment.EquipmentMoniterService;
import com.emep.changzhi.analyse.utils.Constant;
import com.emep.changzhi.analyse.utils.DataValidator;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * @author yangj
 * @Description: 监测设备控制器
 * @date 2018/2/26  10:31
 * @version V1.0
 */
@RestController
@RequestMapping("equipment")
@Api(value="设备controller",tags={"监测设备操作接口"})
public class EquipmentMoniterController {

    @Autowired
    private EquipmentMoniterService equipmentMoniterService;

    /**
     * 新增监测设备
     * @return
     * */
    @ApiOperation(value = "新增监测设备" , notes = "新增监测设备")
    @ApiResponses({
            @ApiResponse(code = 1,message = "操作成功"),
            @ApiResponse(code = 0,message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303,message = "权限不足")})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改时传的id", required = false, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "monitorTypeId", value = "监测类型id", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "equipmentName", value = "设备名称", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "equipmentModel", value = "设备型号", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "port", value = "IP地址", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pwd", value = "设备密码", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "equipmentStatus", value = "设备状态", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "equipmentType", value = "设备类型", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "manufactor", value = "设备厂家", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "sim", value = "sim卡号", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "emissionSourceId", value = "所属监测类型", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "realUploadFrequency", value = "实时数据频率", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "minuteUploadFrequency", value = "分钟数据频率", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "minuteUploadFrequency", value = "用户手机号", required = true, dataType = "String",paramType="query")
     })
    @PostMapping(value = "add")
    public ResultBean<EquipmentMoniter> add(@RequestParam Map<String , Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "monitorTypeId","equipmentName","equipmentModel","port","pwd","equipmentStatus",
                "equipmentType","manufactor","sim", "emissionSourceId","realUploadFrequency","minuteUploadFrequency");
        if(result.getCode() == 1){
            return result.boolAdd(equipmentMoniterService.saveEquipmentMoniter(payload)) ;
         
        }
        return result;
    }


    /**
     * 查询所有监测设备
     * @return
     * */
    @ApiOperation(value="获取监测设备列表", notes="无参数获取所有监测设备信息")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @GetMapping(value = "list")
    protected ResultBean<List<EquipmentMoniter>> findAll(){
        return new ResultBean<List<EquipmentMoniter>>(equipmentMoniterService.getEquipmentMoniterList());
    }

    /**
     * 不带条件的分页查询
     * */
    @ApiOperation(value="分页查询用户", notes="分页无参数查询用户")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
    })
    @GetMapping("paging")
    public ResultBean<Page<EquipmentMoniter>> showUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            Page<EquipmentMoniter> pageSysUserInfo = equipmentMoniterService.findEquipmentMoniterNoCriteria(Integer.parseInt(payload.get("pageIndex").toString()),Integer.parseInt(payload.get("pageSize").toString()));
           return result.ok(pageSysUserInfo);
        }
        return result;
    }


    /**
     * 带条件的分页查询
     * */
    @ApiOperation(value="分页带参数查询用户", notes="分页带参数查询用户")
    @ApiResponses({ @ApiResponse(code = 1, message = "操作成功"),
            @ApiResponse(code = 0, message = "操作失败，服务器内部异常"),
            @ApiResponse(code = 303, message = "权限不足") })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "页数", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "pageSize", value = "数量", required = true, dataType = "String",paramType="query"),
            @ApiImplicitParam(name = "areaCode", value = "行政区划", required = false, dataType = "String",paramType="query"),

    })
    @GetMapping(" paging/query")
    public ResultBean<Page<EquipmentMoniter>> PageUsers(@RequestParam Map<String, Object> payload) {
        ResultBean result = DataValidator.validateMapData(payload, "pageIndex","pageSize");
        if (result.getCode() == Constant.RESULT_CODE_SUCCESS){
            EquipmentMoniter equipmentInfo = new EquipmentMoniter();
            equipmentInfo.setMonitorTyptId(payload.containsKey("monitorTyptId") ?payload.get("monitorTyptId") != null ? payload.get("monitorTyptId").toString():"":"" );
            Page<EquipmentMoniter> pageSysUserInfo = equipmentMoniterService.findBookCriteria(Integer.parseInt(payload.get("pageIndex") .toString()),Integer.parseInt(payload.get("pageSize").toString()),equipmentInfo);
            return result.ok(pageSysUserInfo);
        }
        return result;
    }

    /**
     * 根据id查询监测设备
     * */
    @ApiOperation(value="根据id 查询监测设备", notes="根据监测设备的id来获取监测设备的详细信息")
    @ApiImplicitParam(name = "id", value = "监测设备ID", required = true, dataType = "String", paramType = "path")
    @GetMapping(value = "info")
    public ResultBean<EquipmentMoniter> queryEquipmentById(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1) {
           return result.ok(equipmentMoniterService.findEquipmentMoniterById(payload.get("id").toString())) ;
        }
        return result;
    }


    /**
     * 根据id删除监测设备
     * */
    @ApiOperation(value="删除监测设备", notes="根据设备id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "监测设备ID", required = true, dataType = "String")
    @DeleteMapping(value = "/delete")
    public ResultBean<Boolean> delete(@RequestParam Map<String, Object> payload){
        ResultBean result = DataValidator.validateMapData(payload, "id");
        if (result.getCode() == 1){
            return result.bool(equipmentMoniterService.delete(payload.get("id").toString())) ;
        }
        return result;
    }

}
